/**
 * @license
 * Copyright Alibaba.com All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
 */

import {
  AfterViewInit,
  ComponentFactory,
  ComponentFactoryResolver,
  Directive,
  ElementRef,
  EventEmitter,
  Host,
  Input,
  OnChanges,
  OnDestroy,
  OnInit,
  Optional,
  Output,
  Renderer2,
  SimpleChanges,
  TemplateRef,
  ViewContainerRef
} from '@angular/core';
import {ApesNoAnimationDirective, ApesTSType, isNotNil} from '@shared/apes/core';

import { ApesToolTipBaseComponentLegacy } from './base/apes-tooltip-base-legacy.component';
import { ApesToolTipBaseDirective } from './base/apes-tooltip-base.directive';
import { ApesToolTipComponent } from './apes-tooltip.component';
import { ApesToolTipTrigger } from './apes-tooltip.definitions';
import { Router } from '@angular/router';
import { Subscription } from 'rxjs';

// @ts-ignore
// @ts-ignore
// @ts-ignore
@Directive({
  selector: '[apes-tooltip]',
  exportAs: 'apesToolTip',
  host: {
    '[class.apes-tooltip-open]': 'isToolTipComponentVisible'
  }
})
// @ts-ignore
export class ApesToolTipDirective extends ApesToolTipBaseDirective {
  // tslint:disable-next-line:no-input-rename
  @Input('apesTooltipTitle') specificTitle: ApesTSType;

  /**
   * Use the directive's name as the title that have priority in the second place.
   */
  @Input('apes-tooltip') directiveNameTitle: ApesTSType | null;

  // tslint:disable-next-line:no-input-rename
  @Input('apesTooltipTrigger')  specificTrigger: ApesToolTipTrigger;
  // tslint:disable-next-line:no-input-rename
  @Input('apesTooltipPlacement') specificPlacement: string;

  componentFactory: ComponentFactory<ApesToolTipComponent> = this.resolver.resolveComponentFactory(ApesToolTipComponent);

  private routerSubscription: Subscription;

  constructor(
    elementRef: ElementRef,
    hostView: ViewContainerRef,
    resolver: ComponentFactoryResolver,
    renderer: Renderer2,
    router: Router,
    @Optional() _tooltip?: ApesToolTipBaseComponentLegacy,
    @Host() @Optional() noAnimation?: ApesNoAnimationDirective
  ) {
    super(elementRef, hostView, resolver, renderer, _tooltip, noAnimation);
    // 用于解决切换路由 提示无法消息问题
    this.routerSubscription = router.events.subscribe(() => {
      if (this.tooltipRef){
        this.hide();
      }
    });
  }

  ngOnDestroy() {
    this.routerSubscription.unsubscribe();
  }

}
